#include "vertex_types.fxh"
#include "Samplers.fxh"
#include "globals.fxh"


TNORMAL_TEXTURED_VERTEX_PS RenderVS(TNORMAL_TEXTURED_VERTEX_VS IN)
{
 
    TNORMAL_TEXTURED_VERTEX_PS OUT = (TNORMAL_TEXTURED_VERTEX_PS )0;
    OUT.HPosition=mul(float4(IN.Position,1.0),g_WorldViewProjectionMatrix);
    OUT.UV=IN.UV;
    OUT.WorldNormal=mul(IN.Normal,(float3x3) g_WorldMatrix);
    OUT.WorldPosition=mul(float4(IN.Position,1.0), g_WorldMatrix);
    return OUT;
}

float4 RenderPS(TNORMAL_TEXTURED_VERTEX_PS IN) : COLOR
{

	float g_SpecularPower = 200;
	float g_SpecularMaterialFactor = 0.8;
	float g_DiffuseMaterialFactor = 0.8;
	float4 g_AmbientLight = {0.2, 0.2, 0.2, 1.0};

    float3 Nn=normalize(IN.WorldNormal);
    float3 Hn=normalize(normalize(g_CameraPosition-IN.WorldPosition)-g_LightsDirection[0]);
    float l_SpecularContrib=pow(saturate(dot(Hn, Nn)), g_SpecularPower);
    float3 l_SpecularLighting=l_SpecularContrib*g_LightsColor[0]*g_SpecularMaterialFactor;

    //Diffuse
    float l_DiffuseContrib=saturate(dot(Nn, -g_LightsDirection[0]));
	float3 l_DiffuseLightColor = g_LightsColor[0]*l_DiffuseContrib*g_DiffuseMaterialFactor;
    float3 l_TotalColor=g_AmbientLight+l_DiffuseLightColor+l_SpecularLighting;

    float4 l_color=tex2D(S0LinearSampler,IN.UV);

    return l_color*float4(l_TotalColor ,1);
    
}



technique tecDirectionalLight
{
    pass p0
    {        
        VertexShader =compile vs_3_0 RenderVS();
        PixelShader = compile ps_3_0 RenderPS();
    }
}
